package com.wwtech.epp.ui.adapter.main;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.entity.multi.ParentItem;
import com.wwtech.epp.entity.multi.PersonItem;
import com.wwtech.epp.view.GlideCircleTransform;

import java.util.List;

/**
 * Created by hu on 2017/9/11.
 */

public class CheckManagerAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int TYPE_PARENT=0;
    public static final int TYPE_PERSON=1;

    private OnItemClickListenerme onItemClickListener;

    public CheckManagerAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_PARENT, R.layout.item_contact_parent);
        addItemType(TYPE_PERSON, R.layout.item_contact_person);
    }


    @Override
    protected void convert(BaseViewHolder baseViewHolder, MultiItemEntity item) {
        switch (baseViewHolder.getItemViewType()) {
            case TYPE_PARENT:
                final ParentItem lv0 = (ParentItem)item;
                baseViewHolder.setText(R.id.name, lv0.title);
                ImageView zhishi = baseViewHolder.getView(R.id.iv_zhishi);
                ImageView avater = baseViewHolder.getView(R.id.avater);
                if (lv0.parentType.equals("zuzhi")) {
                    Glide.with(mContext)
                            .load(R.mipmap.ic_launcher)
                            .transform(new GlideCircleTransform(mContext))
                            .into(avater);
                } else {
                    Glide.with(mContext)
                            .load(R.mipmap.mail_group)
                            .transform(new GlideCircleTransform(mContext))
                            .into(avater);
                }
                if (lv0.isExpanded()) {
                    zhishi.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.drop_down));
                } else {
                    zhishi.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.drop_up));
                }
                baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = baseViewHolder.getAdapterPosition();
                        Log.d(TAG, "Level 0 item pos: " + pos);
                        KLog.i(lv0.isExpanded());
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_PERSON:
                final PersonItem lv1 = (PersonItem)item;
                ImageView imageView = baseViewHolder.getView(R.id.avater);
                if (lv1.type.equals("zuzhi")) {
                    baseViewHolder.setText(R.id.name, lv1.node.getName());
                    Glide.with(mContext)
                            .load(R.mipmap.zuzhijiagou)
                            .transform(new GlideCircleTransform(mContext))
                            .into(imageView);
                    baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClickListener.onItemClick(lv1);
                        }
                    });
                }else if (lv1.type.equals("contact")) {
                    baseViewHolder.setText(R.id.name, lv1.freind.getNickname());
                    Glide.with(mContext)
                            .load(lv1.freind.getAvatar())
                            .transform(new GlideCircleTransform(mContext))
                            .into(imageView);
                    baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClickListener.onItemClick(lv1);
                        }
                    });
                } else if (lv1.type.equals("qunzu")) {
                    baseViewHolder.setText(R.id.name, lv1.group.getNickName());
                    Glide.with(mContext)
                            .load(lv1.group.getAvatar())
                            .transform(new GlideCircleTransform(mContext))
                            .into(imageView);
                    baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClickListener.onItemClick(lv1);
                        }
                    });
                }
                break;
        }
    }

    public interface OnItemClickListenerme {
        void onItemClick(PersonItem item);
    }

    public void setOnItemClick(OnItemClickListenerme onItemClick) {
        onItemClickListener = onItemClick;
    }

}
